package com.jacen.fzxs.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jacen.fzxs.entity.RoleEntity;
import com.jacen.fzxs.service.IRightService;
import com.jacen.fzxs.service.IRoleService;
import com.jacen.fzxs.util.DateUtils;
import com.jacen.fzxs.util.ServletUtils;
import com.jacen.fzxs.util.Utils;

/**
 * 角色控制
 * @author Jacen
 *
 */
@Controller
public class RoleController extends BaseController {
	
	@Resource
	private IRoleService roleService;
	@Resource
	private IRightService rightService;
	
	@RequestMapping("/role/list.do")
	public String list(HttpServletRequest request,Integer curPage,ModelMap modelMap) throws Exception{
		Map<String, String> params = ServletUtils.getParameterMap(request);
		List<RoleEntity> list = roleService.getPage(getPager(curPage,request), params);
		modelMap.put("list", list);
		return "role/list";
	}
	
	@RequestMapping(method=RequestMethod.GET,value="/role/insert.do")
	public String enterInsert(){
		return "role/insert";
	}
	
	@RequestMapping(method=RequestMethod.POST,value="/role/insert.do")
	@ResponseBody
	public String postInsert(RoleEntity role){
		try{
			boolean flag = roleService.existByKey("role_name", role.getRoleName());
			if(flag)
				return Utils.errorJsonResp("角色添加异常："+role.getRoleName()+"已存在");
		}catch(Exception e){
			return Utils.errorJsonResp("角色添加异常："+e.getMessage());
		}
		role.setCreateTime(DateUtils.date2Str(new Date(), "yyyy-MM-dd HH:mm:ss"));
		try {
			roleService.insert(role);
		} catch (Exception e) {
			e.printStackTrace();
			return Utils.errorJsonResp("角色添加异常："+e.getMessage());
		}
		return Utils.normalJsonResp();
	}
	
	@RequestMapping(method=RequestMethod.GET,value="/role/update.do")
	public String enterUpdate(String id,ModelMap modelMap){
		RoleEntity entity = roleService.getById(id);
		modelMap.put("entity", entity);
		return "role/update";
	}
	
	@RequestMapping(method=RequestMethod.POST,value="/role/update.do")
	@ResponseBody
	public String postUpdate(RoleEntity entity){
		try {
			RoleEntity role = roleService.getByKey("role_name", entity.getRoleName());
			if(role!=null && !role.getId().equals(entity.getId())){
				return Utils.errorJsonResp("角色修改异常："+entity.getRoleName()+"已存在");
			}
			roleService.update(entity);
		} catch (Exception e) {
			e.printStackTrace();
			return Utils.errorJsonResp("角色修改异常："+e.getMessage());
		}
		return Utils.normalJsonResp();
	}
	
	@RequestMapping(value = "/role/delete.do")
	@ResponseBody
	public String delete(HttpServletRequest request, String id){
		try{
			roleService.deleteById(id);
		}catch(Exception e){
			return Utils.errorJsonResp("删除角色异常："+e.getMessage());
		}
		return Utils.normalJsonResp();
	}
	
	@RequestMapping(method=RequestMethod.GET,value="/role/view.do")
	public String view(String id,ModelMap modelMap){
		RoleEntity entity = roleService.getById(id);
		modelMap.put("entity", entity);
		return "role/view";
	}
	
}
